package com.toocms.jinjuhang.ui.mine.account_management.recharge

import android.app.Application
import android.content.Context
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.PaymentType
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.flow.CreateRechargeOrderData
import com.toocms.jinjuhang.data.order_pay.AppCallbackData
import com.toocms.jinjuhang.data.order_pay.IpaynowData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.ipaynow.Ipaynow
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger
import com.toocms.tab.pay.TabPay
import com.toocms.tab.pay.modle.PayRequest

class RechargeModel(application: Application) : BaseViewModel<BaseModel>(application) {

    val sumObservableField = ObservableField<String>()

    val paymentTypeObservableField = ObservableField(PaymentType.ALI)

    val aliPaymentClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        paymentTypeObservableField.set(PaymentType.ALI)
    })
    val wechatPaymentClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        paymentTypeObservableField.set(PaymentType.WECHAT)
    })
    val bankPaymentClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        paymentTypeObservableField.set(PaymentType.BANK)
    })
    val rechargeClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        createRechargeOrder(sumObservableField.get() ?: "")
    })

    private fun createRechargeOrder(rechargeAmounts: String) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["recharge_amounts"] = rechargeAmounts
        requestData(
            Urls.Center.CREATE_RECHARGE_ORDER,
            params,
            resultClass = CreateRechargeOrderData::class.java,
            result = {
                when (paymentTypeObservableField.get()) {
                    PaymentType.ALI -> aliPayment(it.order_sn)
                    PaymentType.WECHAT -> wechatPayment(it.order_sn)
                    PaymentType.BANK -> {

                    }
                    else -> {

                    }
                }
            })
    }

    private fun wechatPayment(orderSn: String) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["order_sn"] = orderSn
        requestData(Urls.OrderPay.IPAYNOW, params, resultClass = IpaynowData::class.java, result = {
            val message = it.post_data ?: return@requestData
            Ipaynow().wechatPayment(ActivityUtils.getTopActivity() as Context, message) { _, _ ->
                appCallback(orderSn)
            }
        })
    }

    private fun aliPayment(orderSn: String) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["order_sn"] = orderSn
        requestData(
            Urls.OrderPay.ALI_PAYMENT,
            params,
            resultClass = PayRequest::class.java,
            result = {
                TabPay.payV2(this, it) {
                    appCallback(orderSn)
                }
            })
    }


    private fun appCallback(orderSn: String) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["order_sn"] = orderSn
        requestData(
            Urls.OrderPay.APP_CALLBACK,
            params,
            resultClass = AppCallbackData::class.java,
            result = {
                Messenger.getDefault().sendNoMsg(Constants.MessengerToken.TOKEN_REFRESH_BALANCE)
                finishFragment()
            })
    }
}